package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.LiveListAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.SearchLiveListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.List;

public class ZhiboSearchResultActivity extends BaseActivity implements PullToRefreshListView.OnLoadDataListener,PullToRefreshListView.OnRefreshListener{

    private Context mContext;
    private RelativeLayout titleLayout;
    private PullToRefreshListView zhiboList;
    private LiveListAdapter mAdapter;
    private NoDataUtil noDataUtil;
    private String keyword;
    private int size = 20;
    private int currentPage = 1;
    private int searchLiveListTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhibo_search_result);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null){
            keyword = intent.getStringExtra("keyword");
        }
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        zhiboList = (PullToRefreshListView) findViewById(R.id.zhibo_list);
        zhiboList.init(mContext, true);
        mAdapter = new LiveListAdapter(mContext);
        zhiboList.setAdapter(mAdapter, true);
        zhiboList.setOnRefreshListener(this);
        zhiboList.setOnLoadDataListener(this);
        View empty_view = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext, empty_view);
        zhiboList.doRefresh();
    }

    private void getSearchList(){
        if (currentPage ==0){
            zhiboList.setNoNextPage();
            return;
        }
        SearchLiveListTask searchLiveListTask = new SearchLiveListTask(size,currentPage,keyword);
        searchLiveListTask.addListener(mListener);
        searchLiveListTaskId = getEatShowApp().getTaskManager().addTask(searchLiveListTask);

    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (data != null){
                if (currentPage ==1){
                    mAdapter.clearData();
                    zhiboList.onRefreshComplete();
                }else {
                    zhiboList.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> list = (List<EatShowUserInfo>) data;
                if (list != null && list.size()>0){
                    mAdapter.updateData(list);
                }
                if (mAdapter != null && mAdapter.getCount() >0){
                    noDataUtil.ListViewNoEmpty(zhiboList);
                }else {
                    noDataUtil.ListViewEmpty(zhiboList,null,null,-1);
                }
                currentPage = result;
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (currentPage == 1){
                zhiboList.onRefreshComplete();
            }else {
                zhiboList.onLoadDataComplete(false);
            }
        }
    };

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getSearchList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getSearchList();
    }
}
